package com.simpleaccount.Login;

/**
 * Created by zhou on 2017/7/11.
 */

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.simpleaccount.Cloud.CloudOperations;
import com.simpleaccount.MainActivity;
import com.simpleaccount.R;
import com.simpleaccount.Toast.ToastUtils;
import com.simpleaccount.User.AppUser;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.SaveListener;


public class LoginActivity extends Activity implements View.OnClickListener {//class
    private Button btn_login;
    private TextView btn_regist;
    private EditText et_username;
    private EditText et_pwd;
    private String userName;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {//onCreate
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        initView();

    }//onCreate

    private void initView() {//initView
        //对btn进行实例化
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_regist = (TextView) findViewById(R.id.btn_register);
        //edittext进行实例化
        et_username = (EditText) findViewById(R.id.et_username);
        et_pwd = (EditText) findViewById(R.id.et_password);

        btn_login.setOnClickListener(LoginActivity.this);
        btn_regist.setOnClickListener(LoginActivity.this);
    }//initView


    public void onClick(View v) {//onclick
        switch (v.getId()) {//switch
            case R.id.btn_register:
                Intent intent2 = new Intent(this, RegisterActivity.class);
                startActivity(intent2);
                break;

            case R.id.btn_login:
                userName = et_username.getText().toString();
                password = et_pwd.getText().toString();
                if (userName.isEmpty() || password.isEmpty()) {//if empty
                    ToastUtils.toast(this,"用户名或密码不能为空","warning");
                    return;
                }//if empty
//                AppUser bu2 = new AppUser();
//                bu2.setUsername(userName);
//                bu2.setPassword(password);
                BmobUser.loginByAccount(userName, password, new LogInListener<AppUser>() {
                    @Override
                    public void done(AppUser appUser, BmobException e) {
                        if (e == null) {//if e==null
                            ToastUtils.toast(LoginActivity.this, "登陆成功", "success");
                            finish();
                        }//if e==null
                        else {//else e==null
                            if(e.getErrorCode()==9016||e.getErrorCode()==9010){
                                ToastUtils.toast(LoginActivity.this, "网络错误，请检查您的网络", "err");
                                Log.e("login", e.getMessage(),e );
                            }
                            else{
                                Log.e("login", e.getMessage(),e );
                                ToastUtils.toast(LoginActivity.this, "用户名或密码不正确", "err");
                            }

                        }
                    }
                });
                break;
            default:
                break;
        }//switch
    }//onclick

    @Override
    protected void onPause() {
        super.onPause();
        ToastUtils.cancel();
    }

    @Override
    protected void onStop() {
        super.onStop();
        ToastUtils.cancel();
        CloudOperations.SyncData(LoginActivity.this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ToastUtils.cancel();

    }
}//class





